/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    
package be.vdabantwerpen.gow.dao;
    
import be.vdabantwerpen.gow.entities.Post;
import be.vdabantwerpen.gow.entities.Reply;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.repackaged.org.joda.time.DateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Tim.VandenLangenberg
 * @version 0.1
 */
public class ReplyDAOImpl implements ReplyDAO {
    private final DatastoreService datastore = 
            DatastoreServiceFactory.getDatastoreService();
    private final PostDAO postDAO = new PostDAOImpl();
    
    @Override
    public void create(Reply reply){
        Entity entity = toEntity(reply);
        datastore.put(entity);
    }
    
    @Override
    public Reply read(int hashCode){
        Entity e = new Entity("Reply", hashCode);
        try {
            return toReply(datastore.get(e.getKey()));
        }
        catch(EntityNotFoundException enfe){
            return null;
        }
    }
    
    @Override
    public void update(Reply reply){
        Entity entity = toEntity(reply);
        datastore.put(entity);
    }
    
    @Override
    public void delete(Reply reply){
        Entity e = new Entity("Reply", reply.hashCode());
        datastore.delete(e.getKey());
    }
    
    @Override
    public List<Reply> findAll(){
        List<Reply> replies = new ArrayList();
        Query q = new Query("Reply");
        PreparedQuery pq = datastore.prepare(q);
        for(Entity e : pq.asIterable()){
            replies.add(toReply(e));
        }
        return replies;
    }
    
    private Reply toReply(Entity entity){
        Post post;
        if(entity.getProperty("post") == null){
            post = null;
        }
        else {
            post = postDAO.read(Integer.valueOf(entity.getProperty("post").toString()));
        }
        String user;
        if(entity.getProperty("user") == null){
            user = null;
        }
        else {
            user = entity.getProperty("user").toString();
        }
        DateTime date;
        if(entity.getProperty("date") == null){
            date = null;
        }
        else {
            date = DateTime.parse(entity.getProperty("date").toString());
        }
        String content;
        if(entity.getProperty("content") == null){
            content = null;
        }
        else {
            content = entity.getProperty("content").toString();
        }
        return new Reply(post, user, date, content);
    }
    
    private Entity toEntity(Reply reply){
        Entity e = new Entity("Reply", reply.hashCode());
        e.setProperty("post", reply.getPost().hashCode());
        e.setProperty("user", reply.getUser());
        e.setProperty("date", reply.getDate().toString());
        e.setProperty("content", reply.getContent());
        return e;
    }
    
}
